define(function(require, exports, module) {
    var $= require('jquery');
    var Bridge = require('./bridge');

    Bridge.init("http://getimg.aikan.co/iframe.html", "http://getimg.aikan.co");
    //缩略图
    jQuery.fn.LoadImage = function (current, scaling, width, height, loadpic) {

        if (loadpic == null)
            loadpic = "/resource/ui/ui_6/assets/images/wait.png";

        var imgArr = [];
        this.each(function () {
            var src = jQuery(this).attr("code");
            imgArr.push(src);
        });
        var params = "ids=" + imgArr.join();
        var p_url = "http://getimg.aikan.co/img-1-1-3f590ccfbe545c548e5d4adedd85c22c.jpg";

        if(imgArr.length < 1){
            return;
        }
        Bridge.ajax({
            url: p_url,
            data: params,
            type: "POST",
            success: function (response) {

                var e = JSON.parse(response);
                //console.log(e);
                imgs = jQuery(current);
                imgs.each(function () {
                    var t = jQuery(this);
                    var src = 'http://imgcache.aikan.co/' + e[jQuery(this).attr("code")];

                    jQuery(this).attr("src", src);

                    var img = new Image();
                    img.src = src;
                    //自动缩放图片
                    var autoScaling = function () {
                        if (scaling) {
                            if (img.width > 0 && img.height > 0) {
                                if (img.width / img.height >= width / height) {
                                    if (img.width > width) {
                                        t.width(width);
                                        t.height((img.height * width) / img.width);
                                    }
                                    else {
                                        t.width(img.width);
                                        t.height(img.height);
                                    }
                                }
                                else {
                                    if (img.height > height) {
                                        t.height(height);
                                        t.width((img.width * height) / img.height);
                                    }
                                    else {
                                        t.width(img.width);
                                        t.height(img.height);
                                    }
                                }
                            }
                        }
                    };
                    //处理ff下会自动读取缓存图片
                    if (img.complete) {
                        return;
                    }
                    jQuery(this).attr("src", "");
                    var loading = jQuery("<img style=\"height:48px;width:110px;padding-left: 6%;padding-top: 12%;\" alt=\"加载中...\" title=\"图片加载中...\" src=\"" + loadpic + "\" />");
                    t.hide();
                    t.after(loading);
                    jQuery(img).load(function () {
                        loading.remove();
                        t.attr("src", this.src);
                        t.show();
                    });
                });
            }
        });
    };
});




